package com.megalith.controller;

import com.megalith.dto.CandidateProcessDTO;
import com.megalith.dto.CandidateProcessInstanceBaseDTO;
import com.megalith.dto.CandidateProcessInstanceDTO;
import com.megalith.service.CandidateTaskService;
import com.megalith.service.CommonTaskService;
import com.megalith.utils.RequestResponseUtil;
import com.megalith.vo.HistoricActivityInstanceEntityVO;
import com.megalith.vo.HistoricTaskInstanceVO;
import com.megalith.vo.Result;
import com.megalith.vo.TaskEntityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 候选人任务，接口管理
 * @Author lfp
 * @Date 2022/7/15
 * @Version 1.0
 */
@RestController
@Api(tags = "候选人任务，接口管理")
@RequestMapping("task/candidate")
public class CandidateTaskController {

    @Autowired
    private CandidateTaskService candidateTaskService;
    @Autowired
    private CommonTaskService commonTaskService;

    /**
    *@Description 查询指定流程当前办理节点
    *@Author lfp
    *@Date 2022/7/16
    *@param processInstanceId
    *@Return
    */
    @ApiOperation(value = "查询指定流程当前办理节点")
    @GetMapping("process/current/{processInstanceId}")
    public Result<TaskEntityVO> queryProcessCurrentTask(@PathVariable String processInstanceId){
        return Result.ok(commonTaskService.queryProcessCurrentTask(processInstanceId));
    }

    /**
    *@Description 启动候选人流程实例(设置候选人)
    *@Author lfp
    *@Date 2022/7/16
    *@param candidateProcessDTO
    *@param result
    *@Return 
    */
    @ApiOperation(value = "启动候选人流程实例(设置候选人)",
            notes = "流程定义key：candidate,流程定义文件：candidate.bpmn20.xml")
    @PostMapping("process/start")
    public Result startCandidateProcess(@Validated @RequestBody CandidateProcessDTO candidateProcessDTO, BindingResult result){
        if (result.hasErrors()) {
            return RequestResponseUtil.doBindingResultHandel(result);
        }else {
            return candidateTaskService.startCandidateProcess(candidateProcessDTO);
        }
    }


    /**
    *@Description 根据个人Id，查询可以认领的任务列表
    *@Author lfp
    *@Date 2022/7/16
    *@param userId
    *@Return
    */
    @ApiOperation(value = "根据个人Id，查询可以认领的任务列表")
    @GetMapping("personal/list/{userId}")
    public Result<List<TaskEntityVO>> listPersonalCandidateTask(@PathVariable String userId){
        return Result.ok(commonTaskService.listPersonalCandidateTask(userId));
    }

    /**
    *@Description 认领任务
    *@Author lfp
    *@Date 2022/7/16
    *@param processInstanceBaseDTO
    *@param result
    *@Return 
    */
    @ApiOperation(value = "认领任务")
    @PostMapping("claim/task")
    public Result claimCandidateTask(@Validated @RequestBody CandidateProcessInstanceBaseDTO processInstanceBaseDTO, BindingResult result){
        if (result.hasErrors()) {
            return RequestResponseUtil.doBindingResultHandel(result);
        }else {
            return candidateTaskService.claimCandidateTask(processInstanceBaseDTO);
        }
    }


    /**
    *@Description 处理个人任务
    *@Author lfp
    *@Date 2022/7/16
    *@param processInstanceDTO
    *@param result
    *@Return
    */
    @ApiOperation(value = "处理个人任务")
    @PostMapping("process/complete")
    public Result completePersonalProcess(@Validated @RequestBody CandidateProcessInstanceDTO processInstanceDTO, BindingResult result){
        if (result.hasErrors()) {
            return RequestResponseUtil.doBindingResultHandel(result);
        }else {
            return candidateTaskService.completePersonalProcess(processInstanceDTO);
        }
    }

    /**
    *@Description 归还任务
    *@Author lfp
    *@Date 2022/7/16
    *@param processInstanceBaseDTO
    *@param result
    *@Return
    */
    @ApiOperation(value = "归还任务")
    @PostMapping("unclaim/task")
    public Result unclaimCandidateTask(@Validated @RequestBody CandidateProcessInstanceBaseDTO processInstanceBaseDTO, BindingResult result){
        if (result.hasErrors()) {
            return RequestResponseUtil.doBindingResultHandel(result);
        }else {
            return candidateTaskService.unclaimCandidateTask(processInstanceBaseDTO);
        }
    }

    @ApiOperation(value = "根据流程实例编号，查询历史任务信息")
    @GetMapping("history/task/list/{processInstanceId}")
    public Result<List<HistoricTaskInstanceVO>> queryHistoricTaskInfoList(@PathVariable String processInstanceId){
        return Result.ok(commonTaskService.queryHistoricTaskInfoList(processInstanceId));
    }

    @ApiOperation(value = "根据流程实例编号，查询历史活动信息")
    @GetMapping("activity/history/list/{processInstanceId}")
    public Result<List<HistoricActivityInstanceEntityVO>> queryHistoricActivityInfoList(@PathVariable String processInstanceId){
        return Result.ok(commonTaskService.queryHistoricActivityInfoList(processInstanceId));
    }
}
